﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WEB.Model.Entity.Posts;
using System.Data.SqlClient;
using WEB.Utils;
using System.Data;
using WEB.Data;

namespace WEB.Model.Service.Posts
{
    public class PostService
    {
        //添加一篇新闻
        public int save(Post p)
        {
            String sql = @"insert into t_posts ([sid],[p_title] ,[p_title_fu] ,[p_summary] ,[p_content] ,[p_orgin] ,[p_posttime] ,[p_type] ,[p_titleColor] ,[p_bold] ,[p_top] ,[p_scroll] ,[p_pic] ,[p_passwd] ,[p_url])
                                        values (@sid,@ptitle,@ptitlefu,@psummary,@pcontent,@porgin,@pposttime,@ptype,@ptitlecolor,@pbold,@ptop,@pscroll,@ppic,@ppasswd,@purl)";
            SqlParameter para1 = new SqlParameter("@sid", p.sid);
            SqlParameter para2 = new SqlParameter("@ptitle", p.p_title);
            SqlParameter para3 = new SqlParameter("@ptitlefu", p.p_title);
            SqlParameter para4 = new SqlParameter("@psummary", p.p_title);
            SqlParameter para5 = new SqlParameter("@pcontent", p.p_title);
            SqlParameter para6 = new SqlParameter("@porgin", p.p_title);
            SqlParameter para7 = new SqlParameter("@pposttime", p.p_title);
            SqlParameter para8 = new SqlParameter("@ptype", p.p_title);
            SqlParameter para9 = new SqlParameter("@ptitlecolor", p.p_title);
            SqlParameter para10 = new SqlParameter("@pbold", p.p_title);
            SqlParameter para11 = new SqlParameter("@ptop", p.p_title);
            SqlParameter para12 = new SqlParameter("@pscroll", p.p_title);
            SqlParameter para13 = new SqlParameter("@ppic", p.p_title);
            SqlParameter para14 = new SqlParameter("@ppasswd", p.p_title);
            SqlParameter para15 = new SqlParameter("@purl", p.p_title);

            int val = WEB.Utils.DataAccess.ExecuteNonQuery(sql, para1, para2, para3, para4, para5, para6, para7, para8, para9, para10, para11, para12, para13, para14, para15);

            return val;
        }
        //删除一篇新闻

        public bool delPost(int pid)
        {
            String sql = "delete from t_posts where pid=" + pid;
            return false;
        }

        //获取所有的新闻（非单页面）
        public DataTable nGetPosts()
        {
            String sql = "select * from t_posts where p_type=1 order by p_id desc";
            DataSet ds = WEB.Utils.DataAccess.nGetDataSet(sql,null);
            DataTable dt = ds.Tables[0];
            return dt;
        }


        //把DataRow 转换成实体类
        public Post ConvertDataRowToEntity(DataRow dr )
        {
            Post p = null;
            if (dr != null)
            {
                p = new Post();
                if (dr["p_id"] != DBNull.Value)
                {
                    p.p_id = Convert.ToInt32(dr["p_id"].ToString());
                }
                if (dr["aid"] != DBNull.Value)
                {
                    p.aid = Convert.ToInt32(dr["aid"].ToString());
                }
            }
            return new Post();
        }
    }
}